using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Controls.Login
{
    public partial class Login : Controls.DataManagerUserControl
    {
        protected override void ItemChanged()
        {
            Refresh();
        }

        private void Refresh()
        {
            if (CurrentUser != null)
            {
                mviewLogin.SetActiveView(viewLogin);
            }
            else
            {
                mviewLogin.SetActiveView(viewAuthenticated);
                lblUsername.Text = Helpers.StringHelper.SafeFormat(CurrentUser.Username);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CurrentUser = Managers.UserLoginManager.CurrentUser;
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            CurrentUser = Managers.UserLoginManager.Login(txtUsername.Text, txtPassword.Text);
            if (CurrentUser == null)
            {
                lblError.Visible = true;
            }
        }

        protected override void UserLoggedIn(object sender, EventArgs e)
        {
            Refresh();
        }

        protected override void UserLoggedOut(object sender, EventArgs e)
        {
            Refresh();
        }
    }
}